// pages/myReport/myReport.js
import * as echarts from '../../components/ec-canvas/echarts'
var chart = null
var nameList=[];
var resList=[];

function initChart(canvas, width, height, dpr,) {

  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);


  var option = {
    legend: {
      orient: "vertical",
      left: "left",
    //   data: ["新冠肺炎", "感冒", "普通肺炎", "其他"]
      data: nameList
    },
    series: [{
      center: ['50%', '50%'],
      radius: "55%",
    //   data: [{
    //       name: "新冠肺炎",
    //       value: 76.3
    //     },
    //     {
    //       name: "感冒",
    //       value: 10

    //     },
    //     {
    //       name: "普通肺炎",
    //       value: 6

    //     },
    //     {
    //       name: "其他",
    //       value: 7.7

    //     }
    //   ],
      data:resList,

      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowColor: "rgba(0, 0, 0, 0.5)",
          shadowOffsetX: 0
        }
      },
      type: "pie",
      startAngle: 0,
      name: '病症可能：',
      legendHoverLink: true,
      avoidLabelOverlap: true
    }],


    tooltip: {
      formatter: "{a} {b} : {d}%",
      trigger: 'item',
      position: [65, 200]
    }
  };


  chart.setOption(option);
  return chart;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // ec: {
    //   onInit: initChart
    // },
    // maxPercent: "76.3",
    // result: "新冠肺炎"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nameList=[];
    resList=[];

    var item = JSON.parse(options.item);
    var results = item.card_result;
    console.log(item);

    for(var i=0;i<results.length;i++){
        nameList.push(results[i].res);
        resList.push({
            name:results[i].res,
            value:results[i].acc
        })
    }


    this.setData({
        result: item.possible,
        maxPercent: Number(item.acc*100).toFixed(3),
        ec: {
            onInit: initChart
        },
    })
  }
})